﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Diagnostics;
using System.IO;

/// <summary>
/// Summary description for Configuration
/// </summary>
public class Configuration
{
    private ServiceController[] _services;
    private string[] _eventLogs;
    private DiskInfo[] _disks;
    private ProcessInfo[] _processes;
    private bool _showEventLogsOnDashboard;

    static private Configuration _instace;
    static private string _fileName = HttpContext.Current.Server.MapPath("~/dashboard.config");


    static public Configuration Instance
    {
        get
        {
            if (_instace == null)
                _instace = new Configuration();
            return _instace;
        }
    }

    public ServiceController[] Services 
    {
        get { return _services; }        
    }

    public string[] EventLogs
    { 
        get { return _eventLogs; }
    }

    public DiskInfo[] Disks
    {
        get { return _disks; }
    }

    public ProcessInfo[] Processes
    {
        get { return _processes; }
    }

    public bool ShowEventLogsOnDashboard
    {
        get { return _showEventLogsOnDashboard; }
    }

    private Configuration()
    {
       
        XmlDocument doc = new XmlDocument();        
        doc.Load(_fileName);

        //services        
        RefreshServices(doc);
      
        //eventlogs
        RefreshEventLogs(doc);
        
        //disks
        RefreshDisks(doc);

        //processes
        RefreshProcesses(doc);
    }

    private void RefreshProcesses(XmlDocument doc)
    {
        XmlNodeList nodes = doc.SelectNodes("//processes/process");
        _processes = new ProcessInfo[nodes.Count];
        int i = 0;
        foreach (XmlNode node in nodes)
        {
            string name=node.Attributes["name"].Value;
            switch (node.Attributes["type"].Value)
            {
                case "Run":
                    _processes[i] = new ProcessInfo(name, ProcessMonitoringType.Run);
                    break;
                case "NotRun":
                    _processes[i] = new ProcessInfo(name, ProcessMonitoringType.NotRun);
                    break;
                case "MinMemory":
                    _processes[i] = new ProcessInfo(name, ProcessMonitoringType.MinMemory,Int64.Parse(node.Attributes["memory"].Value));
                    break;
                case "MaxMemory":
                    _processes[i] = new ProcessInfo(name, ProcessMonitoringType.MaxMemory, Int64.Parse(node.Attributes["memory"].Value));
                    break;
            }
            i++;
        }
    }

    private void RefreshDisks(XmlDocument doc)
    {        
        XmlNodeList nodes = doc.SelectNodes("//disks/disk");
        _disks = new DiskInfo[nodes.Count];
        int i = 0;
        foreach (XmlNode node in nodes)
        {
            _disks[i] = new DiskInfo(node.Attributes["letter"].Value, Int64.Parse(node.Attributes["warn"].InnerXml), Int64.Parse(node.Attributes["fail"].InnerXml));

            i++;
        }
    }

    public void RefreshDisks()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(_fileName);
        RefreshDisks(doc);
    }

    private void RefreshEventLogs(XmlDocument doc)
    {
        XmlNode node = doc.SelectSingleNode("//eventlogs/@showOnDashboard");
        if ((node.Value == "1") || (node.Value == "true"))
        {
            this._showEventLogsOnDashboard = true;
        }

        XmlNodeList nodes = doc.SelectNodes("//eventlogs/eventlog");
        _eventLogs = new string[nodes.Count];
        int i = 0;
        foreach (XmlNode node2 in nodes)
        {
            _eventLogs[i] = node2.InnerXml;
            i++;
        }
    }

    public void RefreshEventLogs()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(_fileName);
        RefreshEventLogs(doc);
    }

    private void RefreshServices(XmlDocument doc)
    {
        int i = 0;
        XmlNodeList nodes = doc.SelectNodes("//services/service");
        _services = new ServiceController[nodes.Count];
        i = 0;
        foreach (XmlNode node in nodes)
        {
            _services[i] = new ServiceController(node.InnerXml);
            i++;
        }
    }

    public void RefreshServices()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(_fileName);
        RefreshServices(doc);
    }

    public void RefreshProcesses()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(_fileName);
        RefreshProcesses(doc);
    }
}
